//
//  CollectionAssetView.swift
//  JJImagePicker
//
//  Created by 李加建 on 2021/5/31.
//

import UIKit

class AlbumSelectedView: UIView {
    var viewModel = ImagePickerViewModel()
    init(viewModel: ImagePickerViewModel) {
        self.viewModel = viewModel
        super.init(frame: .zero)
        setup()
    }

    var isShow: Bool = false
    var isAnimating: Bool = false

    var didSelectedBlock: (() -> Void)?

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    lazy var customView: UIView = {
        let lazy = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400))
        lazy.backgroundColor = .white
        lazy.clipsToBounds = true
        return lazy
    }()

    lazy var bgView: UIButton = {
        let lazy = UIButton()
        lazy.addTarget(self, action: #selector(bgButtonAction), for: .touchUpInside)
        lazy.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        return lazy
    }()

    override var frame: CGRect {
        didSet {
            bgView.frame = bounds
            customView.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 400)
            collectionView.frame = customView.bounds
        }
    }

    var indicatorView: UIView?

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let width: CGFloat = UIScreen.main.bounds.width
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: width, height: 56)
        layout.scrollDirection = .vertical
        let lazy = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        lazy.delegate = self
        lazy.dataSource = self
        lazy.backgroundColor = .clear
        lazy.showsVerticalScrollIndicator = false
        lazy.showsHorizontalScrollIndicator = false

        lazy.register(AlbumCell.self, forCellWithReuseIdentifier: "AlbumCell")

        return lazy
    }()
}

private extension AlbumSelectedView {
    func setup() {
        addSubview(bgView)
        addSubview(customView)
        customView.addSubview(collectionView)
    }

    @objc
    func bgButtonAction() {
        hide()
    }
}

extension AlbumSelectedView: UICollectionViewDataSource, UICollectionViewDelegate {
    public func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
        viewModel.albumsCount
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell: AlbumCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlbumCell", for: indexPath)
            as? AlbumCell else { fatalError("AlbumCell") }
        cell.backgroundColor = .white
        cell.album = viewModel.assetCollection(for: indexPath.row)
        return cell
    }

    public func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        viewModel.updateSelected(index: indexPath.row)
        didSelectedBlock?()
        hide()
    }
}

extension AlbumSelectedView {
    func show(in view: UIView) {
        if isAnimating {
            return
        }
        isAnimating = true
        collectionView.reloadData()
        view.addSubview(self)
        bgView.alpha = 0
        customView.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 0)

        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut) {
            self.customView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: 400)
            self.bgView.alpha = 1
            self.indicatorView?.layer.transform = CATransform3DMakeRotation(CGFloat(Double.pi), 0.0, 0.0, 1.0)
        } completion: { _ in
            self.isShow = true
            self.isAnimating = false
        }
    }

    func hide() {
        if isAnimating {
            return
        }
        isAnimating = true
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut) {
            self.customView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: 0)
            self.bgView.alpha = 0
            self.indicatorView?.layer.transform = CATransform3DIdentity
        } completion: { _ in
            self.isShow = false
            self.isAnimating = false
            self.removeFromSuperview()
        }
    }
}
